import { ArrayQueue } from "./ArrayQueue";

export function Josephus(names: string[], num: number): any {
  if (names.length < 1) {
    return -1;
  }
  const queue1 = new ArrayQueue<string>();

  // 全部入队
  while (names.length) {
    const name = names.shift();
    if (name) {
      queue1.enQueue(name);
    }
  }
  while (queue1.size() > 1) {
    for (let i = 1; i <= num; i++) {
      if (i < num) {
        // 没数到num时出队再入队，等待继续被数数
        queue1.enQueue(queue1.deQueue()!);
      }
    }
    // 数到num时出队
    queue1.deQueue();
  }

  const outName = queue1.deQueue();
  return outName;
}

console.log(Josephus(["张三", "李四", "王五", "赵六", "钱七"], 3));
console.log(Josephus(["张三", "李四", "王五", "赵六", "钱七"], 1));
console.log(Josephus(["张三", "李四", "王五", "赵六", "钱七"], 4));

export {};
